package com.shinowit.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shinowit.dao.mapper.SysResourceMapper;
import com.shinowit.dao.mapper.SysRoleMapper;
import com.shinowit.model.DataGridResult;
import com.shinowit.model.Result;
import com.shinowit.model.SysResource;
import com.shinowit.model.SysResourceCriteria;
import com.shinowit.model.SysRole;
import com.shinowit.model.SysRoleCriteria;

@Controller
@RequestMapping(value="/system/role")
public class RoleController {
	
	@Resource
	private SysRoleMapper sys_role_mapper_dao; 
	
	@Resource
	private SysResourceMapper sys_resource_mapper_dao;

	@RequestMapping(value={"","/"})
	public String defaultPage(){
		return "/role/list";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Map<String,Object> getPage(@RequestParam(value="page",defaultValue="1") Integer pageIndex,@RequestParam(value="rows",defaultValue="10") Integer pageSize,String roleCode,String roleName,String sort,String order){
		Map<String,Object> result=new HashMap<String,Object>();
		
		SysRoleCriteria ex=new SysRoleCriteria();
		SysRoleCriteria.Criteria cr1= ex.createCriteria();
		
		if ((null!=roleCode) && (roleCode.trim().length()>0)){
			cr1.andSysRoleCodeLike(roleCode+"%");
		}
		
		if ((null!=roleName) && (roleName.trim().length()>0)) {
			cr1.andSysRoleNameLike(roleName+"%");
		}
		
		ex.setPageIndex(pageIndex);
		ex.setPageSize(pageSize);
		
		if ((null!=sort) && (sort.trim().length()>0)){
			ex.setOrderByClause(sort);
		}
		
		if ((null!=order) && (order.trim().length()>0)){
			if (!"asc".equalsIgnoreCase(order)){
				ex.setOrderByClause(ex.getOrderByClause()+" desc");
			}
		}
		
		List<SysRole> data_list=sys_role_mapper_dao.selectByExamplePage(ex);
		
		result.put("rows", data_list);
		
		long record_count=sys_role_mapper_dao.countByExample(ex);
		result.put("total", record_count);
		
		return result;
	}
	
	@RequestMapping(value="/create")
	@ResponseBody
	public Map<String,Object> create_new(SysRole data){
		Map<String,Object> result_map=new HashMap<String,Object>();
		int i=sys_role_mapper_dao.insert(data);
		if (1==i){
			result_map.put("msg", "保存成功!");
			result_map.put("status", true);
		}else{
			result_map.put("msg", "保存失败!");
			result_map.put("status", false);
		}
		return result_map;
	}

	@RequestMapping(value="/edit")
	@ResponseBody
	public Result edit_data(SysRole data){
		Result result=new Result();
		int i=sys_role_mapper_dao.updateByPrimaryKey(data);
		if (1==i){
			result.setMsg("修改成功！");
			result.setStatus(true);
		}else{
			result.setMsg("修改失败！");
			result.setStatus(false);
		}
		return result;
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Result delete_data(String roleCode){
		Result result=new Result();
		int i=sys_role_mapper_dao.deleteByPrimaryKey(roleCode);
		if (1==i){
			result.setMsg("删除成功！");
			result.setStatus(true);
		}else{
			result.setMsg("删除失败！");
			result.setStatus(false);
		}
		return result;
	}	
		
	@RequestMapping(value="/res_tree_list")
	@ResponseBody
	public DataGridResult<SysResource> getAllResourceTree(){
		DataGridResult<SysResource> result=new DataGridResult<SysResource>();
		result.setTotal(sys_resource_mapper_dao.countByExample(new SysResourceCriteria()));
		SysResourceCriteria ex=new SysResourceCriteria();
		ex.createCriteria().andParentCodeIsNull();
		List<SysResource> datas=sys_resource_mapper_dao.selectByExample(ex);
		for (SysResource current_node:datas){
			getChildNode(current_node);
		}
		result.setRows(datas);
		return result;
	}

   private void getChildNode(SysResource parentNode){
		SysResourceCriteria ex=new SysResourceCriteria();
		ex.createCriteria().andParentCodeEqualTo(parentNode.getSysResCode());
		List<SysResource> datas=sys_resource_mapper_dao.selectByExample(ex);
		for (SysResource current_node:datas){
			parentNode.children.add(current_node);
			getChildNode(current_node);
		}	   
   }
}
